home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 August / CHIP Turkiye Ağustos 2001.iso / prog / agustos / 08 / setup.exe / Favorite.java < prev    next >
Text File  |  2001-06-19  |  12KB  |  317 lines

  1. /* Convert MSIE Favourites to HTML page */
  2. /* Author: Brian D. Jones               */
  3. /* Date:   10th Jan 97                  */
  4. /* Email:  Brian_Jones@technologist.com */
  5. // History:    18 May 97 Check for "URL=" since not always last line in file.
  6. //          14 Jun 97 Long names which include blanks now supported.
  7. //          26 Jul 97 Explorer 4.0 aware
  8. //            13 Oct 97 Explorer 4.0 Outline added.
  9. //            20 Nov 99 Change Web site URL.
  10. //          26 May 2001 Sort entries.
  11. //          26 May 2001 Make like an Applet for use in HTML Application GUI
  12.  
  13. import java.io.*;
  14. import java.util.*;
  15. import java.applet.*;
  16. import java.awt.*;
  17. import com.ms.security.*;
  18.  
  19. public class Favorite extends Applet {
  20.     public static String Version = "1.81";
  21.     private static int level = 0;
  22.  
  23.     private static void ListDir( File Path , PrintStream Out) {
  24.     String Names[] = Path.list();
  25.  
  26.     if (folderFirst) sortF(Names, Path);
  27.     else sort(Names);
  28.  
  29.     if (level == 0)
  30.         Out.println("<div id=\"Out" +level +"\">");
  31.     else
  32.         Out.println("<div id=\"Out" +level +"d\" style=\"display:None\">");
  33.     level = level +1;
  34.     Out.println("<DL>");
  35.     for (int i=0; i < Names.length; i++) {
  36.         try {
  37.         File temp = new File( Path, Names[i] );
  38.         if ( temp.isDirectory() ) {
  39.             Out.println( "<DT><div id=\"Out" +level +"\" class=\"Outline\" style=\"cursor: hand\"><IMG SRC=\"" +iconPath +"urlfldr.gif\" WIDTH=16 HEIGHT=16 id=\"Out" +level +"\" class=\"Outline\" hspace=4> "+ temp.getName() +"</div><DD>" );
  40.  
  41.             ListDir ( temp, Out );
  42.             Out.println( "</DD></DT>" );
  43.         }
  44.         else if (temp.getName().endsWith(".url")) {
  45.             try {
  46.             String BaseName = temp.getName();
  47.             String sURL = null,
  48.                 sDesc = null,
  49.                 sAuthor = null,
  50.                 sWhatsNew = null,
  51.                 sAccessed = null, sModified = null;
  52.             DataInputStream file = new DataInputStream( new FileInputStream( temp ));
  53.             String Line= null;
  54.             Date dateModified = null,
  55.                 dateAccessed = null;
  56.  
  57.             while ( file.available() > 0 ) {
  58.                 Line = file.readLine();
  59. //                if (Line.startsWith("URL=")) break; // Not always last line.
  60.                 if (Line.startsWith("URL="))
  61.                     sURL = Line.substring( Line.indexOf( "=" )+1);
  62. //                These items appeared in the Beta, but don't seem to be in the final version.
  63.                 if (Line.startsWith("Desc="))
  64.                     sDesc = Line.substring( Line.indexOf( "=" )+1);
  65.                 if (Line.startsWith("Author="))
  66.                     sAuthor = Line.substring( Line.indexOf( "=" )+1);
  67.                 if (Line.startsWith("WhatsNew="))
  68.                     sWhatsNew = Line.substring( Line.indexOf( "=" )+1);
  69.                 if (Line.startsWith("Accessed="))
  70.                     sAccessed = Line.substring( Line.indexOf( "=" )+1);
  71.                 if (Line.startsWith("Modified="))
  72.                     sModified = Line.substring( Line.indexOf( "=" )+1);
  73.             }
  74.             file.close();
  75. //            Line = Line.substring( Line.indexOf( "=" )+1);
  76.             BaseName = BaseName.substring( 0, BaseName.lastIndexOf( "." ));
  77.             Out.print( "<DT>");
  78.             try {
  79.                 if (sModified != null) {
  80.                     String tmp = null;
  81.                     StringTokenizer token = new StringTokenizer( sModified, ",", false );
  82.                     tmp = token.nextToken().substring(2);
  83.                     dateModified = new Date( Long.parseLong(tmp,16));
  84. //                    Out.println(" Modified: " +dateModified.toLocaleString());
  85.                 }
  86.                 if (sAccessed != null) {
  87.                     String tmp = null;
  88.                     StringTokenizer token = new StringTokenizer( sAccessed, ",", false );
  89.                     tmp = token.nextToken().substring(2);
  90.                     dateAccessed = new Date( Long.parseLong(tmp,16));
  91. //                    Out.println(" Modified: " +dateAccessed.toLocaleString());
  92.                 }
  93.             }
  94.             catch ( Exception e ) { } // Out.println( "<!-- " +e +" -->");}
  95.             Out.print( "<A HREF=\"" +sURL +"\">");
  96.             if ( dateAccessed != null
  97.                 && dateModified != null
  98.                 && dateAccessed.after(dateModified))
  99.                 Out.print( "<IMG SRC=\"" + iconPath + "urlchg.gif\" WIDTH=16 HEIGHT=16 border=0 hspace=4>");
  100.             else
  101.                 Out.print( "<IMG SRC=\"" + iconPath + "url.gif\" WIDTH=16 HEIGHT=16 Border=0 hspace=4>");
  102.             Out.println( BaseName +"</A>");
  103.             if (sWhatsNew != null)
  104.                 Out.println("<DD>What's new: " +sWhatsNew +"</DD>");
  105.             else if (sDesc != null )
  106.                 Out.println("<DD>" +sDesc +"</DD>");
  107.             }
  108.             catch ( Exception e ) { ;
  109.             }
  110.         }
  111.         else if (temp.getName().equalsIgnoreCase("desktop.ini")) {
  112.             try {
  113.             String BaseName = Path.getName();
  114.             String sURL = null,
  115.                 sLogo = "" + iconPath +"url.gif",
  116.                 sWideLogo = null;
  117.             DataInputStream file = new DataInputStream( new FileInputStream( temp ));
  118.             String Line= null;
  119.  
  120.             while ( file.available() > 0 ) {
  121.                 Line = file.readLine();
  122. //                if (Line.startsWith("URL=")) break; // Not always last line.
  123.                 if (Line.startsWith("CDFURL="))
  124.                     sURL = Line.substring( Line.indexOf( "=" )+1);
  125.                 if (Line.startsWith("Logo="))
  126.                     sLogo = Line.substring( Line.indexOf( "=" )+1);
  127.                 if (Line.startsWith("WideLogo="))
  128.                     sWideLogo = Line.substring( Line.indexOf( "=" )+1);
  129.             }
  130.             file.close();
  131.             if (sURL != null) {
  132.                 Out.print( "<A HREF=\"" +sURL +"\">");
  133.                 if (sWideLogo != null)
  134.                     Out.print( "<IMG SRC=\"" +sWideLogo +"\"></A>");
  135.                 else
  136.                     Out.print( "<IMG SRC=\"" +sLogo +"\"></A>");
  137.             }
  138.             }
  139.             catch ( Exception e ) { ;
  140.             }
  141.         }
  142.         }
  143.         catch ( Exception e ) { continue;
  144.         }
  145.     }
  146.     Out.println("<P></DL>");
  147.     Out.println("</div>");
  148.     }
  149.  
  150.     public static void DoIt( String Path, PrintStream Out ) {
  151.  
  152.     File Root = null;
  153.  
  154.     try {
  155.         Root = new File( Path );
  156.     }
  157.     catch  ( Exception e ) {
  158.         Out.println("<HTML><HEAD><TITLE>404 Not Given</TITLE></HEAD>");
  159.         Out.println("<BODY><H1>404 Not Given</H1>No path was requested.<P></BODY></HTML>");
  160.         return;
  161.     }
  162.     if ( !Root.exists()
  163.     || !Root.isDirectory()) {
  164.         Out.println("<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>");
  165.         Out.println("<BODY><H1>404 Not Found</H1>The requested path <EM>" +Path +"</EM> was not found on this server<P></BODY></HTML>");
  166.         return;
  167.     }
  168.      Out.print( "<HTML><HEAD>");
  169.     Out.println( "<TITLE>" +Root.getName() +"</TITLE>");
  170.  
  171.     Out.println("<script language=\"JavaScript\">");
  172.     Out.println("<!--");
  173.     Out.println("function clickHandler() {");
  174.     Out.println("  var targetId, srcElement, targetElement;");
  175.     Out.println("  if (window.event) srcElement = window.event.srcElement;");
  176.     Out.println("  else srcElement = clickHandler.arguments[0].target;");
  177.     Out.println("  if (srcElement.className == \"Outline\") {");
  178.     Out.println("     targetId = srcElement.id + \"d\";");
  179.     Out.println("     if (document.getElementById) targetElement = document.getElementById(targetId);");
  180.     Out.println("     else if (document.all) targetElement = document.all(targetId);");
  181.     Out.println("     else return;");
  182.     Out.println("     if (targetElement.style.display == \"none\") {");
  183.     Out.println("        targetElement.style.display = \"\";");
  184.     Out.println("        document.images(srcElement.id).src = \"" + iconPath + "urlfldro.gif\";");
  185.     Out.println("        setCookie( targetId, \"show\" );");
  186.     Out.println("     } else {");
  187.     Out.println("        targetElement.style.display = \"none\";");
  188.     Out.println("        document.images(srcElement.id).src = \"" + iconPath + "urlfldr.gif\";");
  189.     Out.println("        setCookie( targetId, \"none\" );");
  190.     Out.println("     }");
  191.     Out.println("  }");
  192.     Out.println("}");
  193.     Out.println("");
  194.     Out.println("function setCookie(name, value) {");
  195.     Out.println("  document.cookie = name + \"=\" + escape(value);");
  196.     Out.println("}");
  197.     Out.println("function readtree() {");
  198.     Out.println("  var dc = document.cookie;");
  199.     Out.println("  if (dc.length > 0) {");
  200.     Out.println("    ca = dc.split(\";\");");
  201.     Out.println("    for ( c in ca ) {");
  202.     Out.println("      mid = ca[c].indexOf(\"=\");");
  203.     Out.println("      if (ca[c].charAt(0) == \" \") targetId = ca[c].substring(1,mid)");
  204.     Out.println("      else  targetId = ca[c].substring(0,mid)");
  205.     Out.println("      if ( targetId.substr(0,3) != \"Out\") continue;");
  206.     Out.println("      val = ca[c].substr(mid+1,4);");
  207.     Out.println("      if (document.getElementById) targetElement = document.getElementById(targetId);");
  208.     Out.println("      else if (document.all) targetElement = document.all(targetId);");
  209.     Out.println("      else return;");
  210.     Out.println("      if (val == \"show\") {");
  211.     Out.println("        targetElement.style.display = \"\";");
  212.     Out.println("        document.images(targetId.substr(0,targetId.length-1)).src = \"" + iconPath + "urlfldro.gif\";");
  213.     Out.println("      }");
  214.     Out.println("    }");
  215.     Out.println("  }");
  216.     Out.println("}");
  217.     Out.println("document.onclick = clickHandler;");
  218.     Out.println("//-->");
  219.     Out.println("</script>");
  220.     Out.println( "<style type=\"text/css\">");
  221.     Out.println(" body { font-family: \"verdana,arial,helvetica\"; font-size:10pt;}");
  222.     Out.println(" .head { font-size:14pt; font-weight: bold;}");
  223.     Out.println(" .OutLine { font-weight: bold; }");
  224.     Out.println("</style>");
  225.     Out.println( "</HEAD>" );
  226.  
  227.     Out.print("<body onload=\"readtree();\">");
  228.     Out.println( "<div class=\"head\"><IMG SRC=\"" + iconPath + "urlfldr.gif\" WIDTH=32 HEIGHT=32>" +Root.getName() +"</div><HR>");
  229.  
  230.         ListDir( Root, Out );
  231.  
  232.     Out.println( "<HR>" );
  233.     Out.println( "Created: " +new Date().toLocaleString() );
  234.     Out.println( "using Java application v: " + Version + " created by <A HREF=\"http://www.jonessoft.free-online.co.uk\">Brian D. Jones</A>" );
  235.     Out.println( "<A HREF=\"mailto:Brian_Jones@technologist.com\">Email: Brian_Jones@technologist.com</A>" );
  236.     Out.println( "</BODY>" );
  237.     Out.println( "</HTML>" );
  238.  
  239.     }
  240.  
  241.     private static String cmdLine( String args[]) {
  242.         String returnString = args[0];
  243.         for ( int i=1; i< args.length; i++) returnString = returnString +" " +args[i];
  244.         return returnString;
  245.     }
  246.  
  247.     private static boolean folderFirst = false;
  248.     private static String iconPath = "/icons/";
  249.     private static void sort( String toSort[]) {
  250.         if (toSort.length<2) return;
  251.         int i,j;
  252.         for (i=0; i < toSort.length ; i++)
  253.             for (j=i+1; j < toSort.length ; j++) 
  254.                 if (toSort[j].compareTo(toSort[i])< 0) {
  255.                     String temp = toSort[i];
  256.                     toSort[i] = toSort[j];
  257.                     toSort[j] = temp;
  258.                 }
  259.     }
  260.  
  261.     private static void sortF( String toSort[], File Path) {
  262.         if (toSort.length<2) return;
  263.         int i,j;
  264.         for (i=0; i < toSort.length ; i++) {
  265.             File fileA = new File( Path, toSort[i] );
  266.             for (j=i+1; j < toSort.length ; j++) {
  267.                 File fileB = new File( Path, toSort[j]);
  268.                 if ( (fileB.isDirectory() && fileA.isFile() )
  269.                 || ( ((fileA.isDirectory() && fileB.isDirectory() )
  270.                 ||   (fileA.isFile() && fileB.isFile()) )
  271.                 && (toSort[j].compareTo(toSort[i])< 0) ) ) {
  272.                     String temp = toSort[i];
  273.                     toSort[i] = toSort[j];
  274.                     toSort[j] = temp;
  275.                     fileA = fileB;
  276.                 }
  277.             }
  278.         }
  279.     }
  280.  
  281.     public void init()
  282.     {
  283.         setForeground( Color.black  );
  284.         setBackground( Color.green  );
  285.     }
  286.  
  287.     public void setOrder( boolean order ) {
  288.         folderFirst = order;
  289.     }
  290.     public void setPath( String Path ) { 
  291.         if (Path==null) iconPath = "";
  292.         else iconPath = Path.replace('\\','/');
  293.     }
  294.     public void save(String folder, String toFile) {
  295.     PolicyEngine.assertPermission(PermissionID.SYSTEM);
  296.         FileOutputStream saveFile = null;
  297.         level = 0;
  298.         try {
  299.             saveFile = new FileOutputStream (toFile) ;
  300.         DoIt( folder, new PrintStream( saveFile ));
  301.         saveFile.close();
  302.         } catch (Exception e) {}
  303.     }
  304.     public static void main (String args[] ) {
  305.     System.out.println("Content-type: text/html");
  306.     System.out.println("");
  307.     if (args.length < 1) {
  308.         System.out.println("<HTML><HEAD><TITLE>404 Not Given</TITLE></HEAD>");
  309.         System.out.println("<BODY><H1>404 Not Given</H1>No path was requested.<P></BODY></HTML>");
  310.         return;
  311.     }
  312.  
  313.     DoIt( cmdLine( args ), System.out );
  314.     }
  315. }
  316.  
  317.